<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/CustomCommand.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/DaoFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Account.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/AccountingEntry.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Bookkeeping.php';
	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	/*
	* Class to create Account
	*/
	class CommandCreateAccount extends CustomCommand {
		/*
		* Constructor of the class
		*/
		public function __construct ($name, 
				$description, $mount, $type) {
		
		    $this->mount=$mount;
		
			$GLOBALS['debug']->log("entro al comando account");
			/**
			 * Creating a Account
			 */
			 $this -> account = new Account($type, $name, $description);
			$GLOBALS['debug']->log("creo el objeto account");	 
			
			/**
			 * Creating a AccountingEntry
			 */
			 $this -> accountingEntry = new AccountingEntry(date("Y-m-d",time()));
			 $GLOBALS['debug']->log("creo el objeto accountingEntry");	
			 /**
			 * Creating a Bookkeeping
			 falta aclarar
			 */
			 	 $GLOBALS['debug']->log($type,"TIPO");	
			 	
				if(($type=="a")||($type=="e"))	
				{
					 $GLOBALS['debug']->log("entro al if del tipo a o e");	
			 	 $this -> bookkeeping = new Bookkeeping( $this->mount,0,$this->accountingEntry->getPkNumber(),$this->account->getPkNumber(),NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
			 		$GLOBALS['debug']->log("creo el objeto bookeeping");	
				}
				if(($type=="p")||($type=="i"))
				{
					$GLOBALS['debug']->log("entro al if del tipo p o i");	
				$this -> bookkeeping = new Bookkeeping(0, $this->mount,$this->accountingEntry->getPkNumber(),$this->account->getPkNumber(),NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
			 	$GLOBALS['debug']->log("creo el objeto bookeeping")	;
				}
			
		
		}
		
		/*
		* Method to execute to create Account
		*/
		public function execute() {
		
			/**
			 * Creating a AccountingEntry
			 */
			$GLOBALS['debug']->log($this ->accountingEntry,"fecha");	
			$command = CommandFactory :: createCommandCreateAccountingEntry($this ->accountingEntry);
			$command -> execute();
			$GLOBALS['debug']->log("ejecuto el comando accountEntry");	
		
			/**
			 * Creating a Account
			 */
			$GLOBALS['debug']->log($this -> account,"Objeto account");	
			$daoFactory= DaoFactory:: getMySqlDaoFactory();
			$iuad= $daoFactory-> getMySqlAccountAccessDao();			
			$success=$iuad->createAccount( $this -> account);
	
			
			$GLOBALS['debug']->log("ejecuto el comando account");	
	
			 /**
			 * Creating a Bookkeeping
			 */
			
			if(($this->account->getType()=="a")||($this->account->getType()=="e"))	
				{
					
					$GLOBALS['debug']->log( $this->mount,"monto");	
					$GLOBALS['debug']->log(0,"valor");	
					$GLOBALS['debug']->log($this->accountingEntry->getPkNumber(),"pk number accountingEntry");	
					$GLOBALS['debug']->log($this->account->getPkNumber(),"pk number account");	
					
					
					$command = CommandFactory :: createCommandCreateBookeeping( $this->mount,0,$this->accountingEntry->getPkNumber(),$this->account->getPkNumber(),NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
					$command -> execute();
					$GLOBALS['debug']->log("ejecuto el comando bookeeping");	
				}
			if 	(($this->account->getType()=="p")||($this->account->getType()=="i"))
				{
					$command = CommandFactory :: createCommandCreateBookeeping(0, $this->mount,$this->accountingEntry->getPkNumber(),$this->account->getPkNumber(),NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);				
				$command -> execute();
				$GLOBALS['debug']->log("ejecuto el comando bookkeeping");	
				}
				
				
		$this -> result = $success;		
		}
	}
?>